﻿using UnityEngine;
using System.Collections;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using System;

public class 广告集成 : MonoBehaviour
{

    public int m_底部广告切换周期 = 50;
    public bool m_底部广告已显示 = false;
    private int m_上次玩家所在行数 = 0;
    public static 广告集成 m_当前广告集成;

    private static string id_底部广告 = "";
    private static string id_插页广告 = "";
    private static string id_激励广告 = "";
    private static string id_原生中屏大广告 = "";

    public static RewardBasedVideoAd ad_视频广告 = null;
    private static BannerView ad_横幅广告;
    private static InterstitialAd ad_全屏广告;
    private static NativeExpressAdView ad_原生广告;
    static AdRequest request_请求参数;
    void Start()
    {
        if (ad_视频广告 == null)
        {
            Debug.Log("start unity demo-------------");

#if UNITY_EDITOR || UNITY_ANDROID
            id_底部广告 = "ca-app-pub-3682424280630333/1289251701";
            id_插页广告 = "ca-app-pub-3682424280630333/4084604307";
            id_激励广告 = "ca-app-pub-3682424280630333/3190857865";
            id_原生中屏大广告 = "ca-app-pub-3682424280630333/2537556053";
#elif UNITY_IPHONE
#else
#endif
            request_请求参数 = new AdRequest.Builder()
                                    //.AddTestDevice(AdRequest.TestDeviceSimulator)
                                    .SetGender(Gender.Male)
                                    .TagForChildDirectedTreatment(true)
                                    .AddKeyword("game")
                                    //.AddExtra("excl_cat", "cars,sports") // Category exclusions for DFP.
                                    .Build();

            ad_横幅广告 = new BannerView(id_底部广告, AdSize.SmartBanner, AdPosition.Bottom);
            ad_横幅广告.OnAdLoaded += 横幅广告_OnAdLoaded;// Called when an ad request has successfully loaded.
            ad_横幅广告.OnAdFailedToLoad += 横幅广告_OnAdFailedToLoad;// Called when an ad request failed to load.
            ad_横幅广告.OnAdOpening += 横幅广告_OnAdOpening;// Called when an ad is clicked.
            ad_横幅广告.OnAdClosed += 横幅广告_OnAdClosed;// Called when the user returned from the app after an ad click.
            ad_横幅广告.OnAdLeavingApplication += 横幅广告_OnAdLeavingApplication;  // Called when the ad click caused the user to leave the application.

            ad_横幅广告.LoadAd(request_请求参数);

            ad_全屏广告 = new InterstitialAd(id_插页广告);
            ad_全屏广告.OnAdLeavingApplication += 全屏广告_OnAdLeavingApplication;
            ad_全屏广告.LoadAd(request_请求参数);

            ad_原生广告 = new NativeExpressAdView(id_原生中屏大广告, new AdSize(280, 320), AdPosition.Center);
            ad_原生广告.OnAdLeavingApplication += 原生广告_OnAdLeavingApplication;

            ad_视频广告 = RewardBasedVideoAd.Instance;
            ad_视频广告.OnAdLoaded += 视频广告_OnAdLoaded;  // has failed to load.
            ad_视频广告.OnAdFailedToLoad += 视频广告_OnAdFailedToLoad; // has failed to load.
            ad_视频广告.OnAdOpening += 视频广告_OnAdOpening;  // is opened.
            ad_视频广告.OnAdStarted += 视频广告_OnAdStarted; // has started playing.
            ad_视频广告.OnAdRewarded += 视频广告_OnAdRewarded; // has rewarded the user.
            ad_视频广告.OnAdClosed += 视频广告_OnAdClosed;  // is closed.
            ad_视频广告.OnAdLeavingApplication += 视频广告_OnAdLeavingApplication;// is leaving the application.
            ad_视频广告.LoadAd(request_请求参数, id_激励广告);

        }
        if (m_当前广告集成 == null)
            m_当前广告集成 = this;
    }


    private void FixedUpdate()
    {

        //if (Input.GetKeyUp(KeyCode.Escape))
        //{
        //    Debug.Log(KeyCode.Escape + "-----------------");
        //}

        if (m_上次玩家所在行数 != 地图管理器.m_当前地图管理器.m_玩家当前所在行)
        {
            m_上次玩家所在行数 = 地图管理器.m_当前地图管理器.m_玩家当前所在行;

            if ( (地图管理器.m_当前地图管理器.m_玩家当前所在行+ m_底部广告切换周期) % m_底部广告切换周期 == 5 )
            {
                Debug.Log("玩家当前所在行 " + 地图管理器.m_当前地图管理器.m_玩家当前所在行);
                m_底部广告已显示 = !m_底部广告已显示;

                if (m_底部广告已显示)
                {
                    显示底部广告();
                }
                else
                {
                    关闭底部广告();
                }
            }
        }
        else
        {
            m_上次玩家所在行数 = 地图管理器.m_当前地图管理器.m_玩家当前所在行;
        }

    }


    public void 显示底部广告()
    {
        Debug.Log("显示底部广告");
        ad_横幅广告.Show();
    }
    public void 关闭底部广告()
    {
        Debug.Log("关闭底部广告");
        ad_横幅广告.Hide();
    }

    public void 显示暂停广告()
    {
        Debug.Log("显示暂停广告");
        ad_原生广告.LoadAd(request_请求参数);
        ad_原生广告.Show();
    }
    public void 关闭暂停广告()
    {
        Debug.Log("关闭暂停广告");
        ad_原生广告.Hide();
    }
    public void 显示全屏广告()
    {
        Debug.Log("全屏广告");
        if (ad_全屏广告.IsLoaded())
        {
            ad_全屏广告.Show();
        }

    }

    public static void 显示视频广告()
    {
        Debug.Log("显示视频广告");

        if (ad_视频广告.IsLoaded())
        {
            ad_视频广告.Show();
        }
    }

    public void 关闭广告()
    {
        关闭底部广告();
        关闭暂停广告();
    }


    private void 横幅广告_OnAdLeavingApplication(object sender, EventArgs e)
    {
        //Firebase.Analytics.FirebaseAnalytics.LogEvent("AD_Event", "AD_Banner_Leaving", 1.0f);
        print("HandleOnAdLeavingApplication");
    }
    
    private void 横幅广告_OnAdOpening(object sender, EventArgs e)
    {
        print("HandleOnAdOpened");
    }
    private void 横幅广告_OnAdClosed(object sender, EventArgs e)
    {
        print("HandleOnAdClosed");
    }

    private void 横幅广告_OnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("Interstitial Failed to load: " + args.Message);
    }

    private void 横幅广告_OnAdLoaded(object sender, EventArgs e)
    {
        print("OnAdLoaded event received.");
    }


    private void 全屏广告_OnAdLeavingApplication(object sender, EventArgs e)
    {
        //Firebase.Analytics.FirebaseAnalytics.LogEvent("AD_Event", "AD_Full_Leaving", 1.0f);
    }
    private void 原生广告_OnAdLeavingApplication(object sender, EventArgs e)
    {
        //Firebase.Analytics.FirebaseAnalytics.LogEvent("AD_Event", "AD_Native_Leaving", 1.0f);
    }

    private void 视频广告_OnAdLoaded(object sender, EventArgs args)
    {}
    private void 视频广告_OnAdFailedToLoad(object sender, EventArgs args)
    { }
    private void 视频广告_OnAdOpening(object sender, EventArgs args)
    {
    }
    private void 视频广告_OnAdStarted(object sender, EventArgs args)
    { }
    private void 视频广告_OnAdClosed(object sender, EventArgs args)
    {
        地图管理器.m_当前地图管理器.m_续命按钮.SetActive(false);
    }

    public static Boolean 视频广告_是否执行回调 = false;
    private void 视频广告_OnAdRewarded(object sender, Reward args)
    {
        //Firebase.Analytics.FirebaseAnalytics.LogEvent("AD_Event", "AD_Rewarded", 1.0f);
        string type = args.Type;
        double amount = args.Amount;
        print("User rewarded with: " + amount.ToString() + " " + type);

        地图管理器.m_当前地图管理器.m_续命按钮.SetActive(false);

        地图管理器.m_当前地图管理器.m_当前玩家控制.m_最新奖励 = (视频广告_是否执行回调) ? (int)(10) : 0 ;
        视频广告_是否执行回调 = false;
    }
    private void 视频广告_OnAdLeavingApplication(object sender, EventArgs args)
    {
        //Firebase.Analytics.FirebaseAnalytics.LogEvent("AD_Event", "AD_Video_Leaving", 1.0f);
    }
}
